package com.lvfeng.controller;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSONObject;
import com.lvfeng.conf.API;
import com.lvfeng.tool.EncryptUtil;
import com.lvfeng.tool.NetTool;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理聚合数据接口请求
 */
@RestController("/juhe")
public class JuheDataController{
    //02001.短信接口  https://www.juhe.cn/docs/api/id/54
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/msg",method = RequestMethod.POST)
    public Map sendMessage(@RequestParam("tel") String tel,@RequestParam("content") String content) throws Exception{
        return new HashMap();
    }
    //02002.周公解梦  https://www.juhe.cn/docs/api/id/64
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "/dream")
    public Map getDream(@RequestParam("content") String content) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");    // 解密
        Map req_param = new HashMap();  // 请求预制参数
        req_param.put("key", des.deCode(API.JUHE_DREAM_KEY));
        req_param.put("q", content);    // 关键字
        req_param.put("cid", "");   // 分类
        req_param.put("full", "");   // 详细
        String res = NetTool.getNetData("http://v.juhe.cn/dream/query", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        return obj.getInnerMap();
    }
    //02003.成语词典  https://www.juhe.cn/docs/api/id/157
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "/dictionary")
    public Map getDictionary(@RequestParam("content") String content) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_DICTIONARY_KEY));
        req_param.put("word", content);
        req_param.put("dtype","json");
        String res = NetTool.getNetData("http://v.juhe.cn/chengyu/query", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
    //02004.新华字典  https://www.juhe.cn/docs/api/id/156
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "xinhua")
    public Map getXinHua(@RequestParam("content") String content) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_XINHUA_KEY));
        req_param.put("dtype","json");
        req_param.put("word",content);
        String res = NetTool.getNetData("http://v.juhe.cn/xhzd/query", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
    //02005.微信精选  https://www.juhe.cn/docs/api/id/147
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "wepick")
    public Map getWeChatPick(@RequestParam("page") int page) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_WEIXIN_KEY));
        req_param.put("ps", 10); // 每页条数
        req_param.put("pno", page); // 页数
        req_param.put("dtype","json");
        String res = NetTool.getNetData("http://v.juhe.cn/weixin/query", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
    //02006.老黄历  https://www.juhe.cn/docs/api/id/65
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "calendar")
    public Map getOldCalendar() throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_CALENDAR_KEY));
        req_param.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date())); // 当前日期
        String res = NetTool.getNetData("http://v.juhe.cn/laohuangli/d", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
    //02007.二维码生成  https://www.juhe.cn/docs/api/id/296
    //02008.彩票开奖查询  https://www.juhe.cn/docs/api/id/300
    //02009.手机号归属地  https://www.juhe.cn/docs/api/id/11
    //02010.新闻头条  https://www.juhe.cn/docs/api/id/235
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "news")
    public Map getNews(@RequestParam("type") String type) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_NEWS_KEY));
        req_param.put("type", type);    // 新闻类型 top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
        String res = NetTool.getNetData("http://v.juhe.cn/toutiao/index", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
    //02011.笑话大全 https://www.juhe.cn/docs/api/id/95
    @ResponseBody
    @CrossOrigin
    @GetMapping(value = "joke")
    public Map getJokeNewest(@RequestParam("page") int page) throws Exception{
        EncryptUtil des = new EncryptUtil(API.ENCRYPT_KEY, "utf-8");
        Map req_param = new HashMap();
        req_param.put("key", des.deCode(API.JUHE_JOKE_KEY));
        req_param.put("page", page);    // 页码
        req_param.put("pagesize", 1);   // 每页几条
        String res = NetTool.getNetData("http://v.juhe.cn/joke/content/text.php", req_param, "GET");
        JSONObject obj = JSONObject.parseObject(res);
        Map map = obj.getInnerMap();
        return map;
    }
}
